#include <stdio.h>
#include <stdlib.h>

// 函数：将字节转换为二进制字符串
void byte_to_binary(char byte, char* binary_str) {
    for (int i = 7; i >= 0; --i) {
        binary_str[7 - i] = ((byte & (1 << i))? '1' : '0');
    }
    binary_str[8] = '\0';
}

// 函数：将8位字节串转换为位串
void bytes_to_bitstring(char* bytes, int mlen, char* bitstring) {
    int index = 0;
    for (int i = 0; i < mlen; ++i) {
        char binary[9];
        byte_to_binary(bytes[i], binary);
        for (int j = 0; j < 8; ++j) {
            bitstring[index++] = binary[j];
        }
    }
    bitstring[index] = '\0';
}

int main() {
    // 示例字节串
    char bytes[] = {0x12, 0x34, 0x56};
    int mlen = sizeof(bytes) / sizeof(bytes[0]);
    char* bitstring = (char*)malloc(mlen * 8 + 1);

    bytes_to_bitstring(bytes, mlen, bitstring);

    printf("位串: %s\n", bitstring);

    free(bitstring);
    return 0;
}
